package com.alks.function.schedule;

import com.alks.common.aop.RedisLock;
import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.service.biocontrol.factoryQuery.IStkAccService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;



/**
 * @author MS
 * @since 2024/11/19下午3:30
 * 生控定时任务
 */
@Component
@RequiredArgsConstructor
@Slf4j
@EnableScheduling
@EnableAsync
public class BiocontrolTask {
    private final IStkAccService stkAccService;

    /**
     * 更新库存账目数据
     * 时间：每日0点半更新昨天的数据
     * 更新要花费20秒左右
     */
    @RedisLock(key = "task:BiocontrolTask.updateforced")
    @Scheduled(cron = "0 10 0 * * ?")
    public void updateForced() {
        Integer day = 1;
        stkAccService.updateForced(day,0);
    }

    /**
     * 测试：早上6点更新
     */
    @RedisLock(key = "task:BiocontrolTask.updateforced")
    @Scheduled(cron = "0 30 0 * * ?")
    public void updateForced2() {
        Integer day = 1;
        stkAccService.updateForced(day,0);
    }

    /*
     * 日报表数据
     * 每天1点更新一下从昨天开始的31天的数据
     */
    @Async("TaskExecutor")
    @Transactional
    @RedisLock(key = "task:BiocontrolTask.updateforced")
    @Scheduled(cron = "0 0 1 * * ?")
    public void ScheduleUpdateCuttingDailyReportForMonth() throws ServiceErrorException {
        log.info("定时任务执行开始,每天1点更新一下从昨天开始的31天的数据 (日报表数据) 更新  开始：");
        //更新冲裁，工艺，配套日报表数据
        stkAccService.updateForced(31,1);
        log.info("定时任务执行成功, 每天1点更新一下从昨天开始的31天的数据 (日报表数据) 更新  结束");
    }

}
